const app = getApp()

import axios from '../../utils/axios'

Page({

  data: {
    firstIndex: 0, // 当前选中的一级分类
    lastIndex: 0, // 当前选中的二级分类
    firstTypeList: [], // 一级分类list
    nextTypeList: [], // 二级分类list
    commodityList: [],
    pageIndex: 1,
    paramShoppJson: {}, // commodity组件传过来的JSON
  },

  onLoad: function (options) {
    this.selectFirstType()

    console.log("页面跳转带过来的值", options)
  },

  onShow: function () {
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      // shoppCountFlag为true时，说明有物品被加入购物车，需要重新刷新数量
      if (app.globalData.shoppCountFlag) {
        app.globalData.shoppCountFlag = false
        axios('/groupon/goodsCart').then(res => {
          if (res.data.resultcode == 1) {
            app.globalData.shoppCount = res.data.num
            this.getTabBar().setData({
              shoppCount: res.data.num
            })
          }
        })
      } else {
        this.getTabBar().setData({
          shoppCount: app.globalData.shoppCount
        })
      }
      this.getTabBar().setData({
        selected: 1
      })
    }

    if (app.globalData.checkNav == "喵星人" && app.globalData.checkFlag) {
      app.globalData.checkFlag = false
      this.setData({
        firstIndex: 1
      })
      this.selectNextType()
    } else if (app.globalData.checkNav == "汪星人" && app.globalData.checkFlag) {
      app.globalData.checkFlag = false
      this.setData({
        firstIndex: 0
      })
      this.selectNextType()
    }

  },

  // 接受commodity组件传过来的JSON
  paramShopp: function (e) {
    
    this.setData({
      paramShoppJson: e.detail
    })
  },

  // 点击一级分类
  cutFirstType: function (e) {
    let index = e.currentTarget.dataset.index
    if (index == this.data.firstIndex) {
      return
    }
    this.setData({
      firstIndex: index,
      lastIndex: 0,
      pageIndex: 1
    })
    this.selectNextType()
  },

  // 点击二级分类
  cutLastType: function (e) {
    let index = e.currentTarget.dataset.index
    if (index == this.data.lastIndex) {
      return
    }
    this.setData({
      lastIndex: index,
      pageIndex: 1
    })
    this.selectGoodsList()
  },

  // 查询第一级分类
  selectFirstType: function () {
    axios('/groupon/goodsType').then(res => {
      console.log(res)
      if (res.data.resultcode == 1) {
        this.setData({
          firstTypeList: res.data.result
        })
        this.selectNextType()
      }
    })
  },

  // 查询二级分类
  selectNextType: function () {

    let pid = this.data.firstTypeList[this.data.firstIndex].type_id

    axios('/groupon/goodsType', {
      pid: pid
    }).then(res => {
      console.log(res)
      if (res.data.resultcode == 1) {
        this.setData({
          nextTypeList: res.data.result
        })
        this.selectGoodsList()
      }
    })
  },

  // 查询商品
  selectGoodsList: function () {

    let paramJson = {
      type_id: this.data.nextTypeList[this.data.lastIndex].type_id,
      page: this.data.pageIndex
    }

    axios('/groupon/goodsList', paramJson).then(res => {
      console.log(res)
      if (res.data.resultcode == 1) {
        if (res.data.result.length < 5) {
          this.setData({
            showPull: true
          })
        }

        if (this.data.pageIndex == 1) {
          this.setData({
            commodityList: app.disposeList(res.data.result)
          })
        } else {
          this.setData({
            commodityList: this.data.commodityList.concat(app.disposeList(res.data.result))
          })
        }


      }
    })
  },

  nextCommodity: function () {
    this.setData({
      pageIndex: ++this.data.pageIndex
    })
    this.selectGoodsList()
  },

  // 搜索点击事件
  checkSearch: function () {
    wx.navigateTo({
      url: '/pages/searchBox/searchBox',
    })
  },

  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})